package client

import (
	"github.com/gin-gonic/gin"
	"likeadmin/admin/schemas/req"
	"likeadmin/admin/service/client"
	"likeadmin/core"
	"likeadmin/core/request"
	"likeadmin/core/response"
	"likeadmin/middleware"
	"likeadmin/util"
)

var PayConfigsGroup = core.Group("/client", newPayConfigsHandler, regPayConfigs, middleware.TokenAuth())

func newPayConfigsHandler(srv client.IPayConfigsService) *payConfigsHandler {
	return &payConfigsHandler{srv: srv}
}

func regPayConfigs(rg *gin.RouterGroup, group *core.GroupBase) error {
	return group.Reg(func(handle *payConfigsHandler) {
		rg.GET("/pay_configs/all", handle.all)
		rg.GET("/pay_configs/list", handle.list)
		rg.GET("/pay_configs/detail", handle.detail)
		rg.POST("/pay_configs/add", handle.add)
		rg.POST("/pay_configs/edit", handle.edit)
		rg.POST("/pay_configs/del", handle.del)
	})
}

type payConfigsHandler struct {
	srv client.IPayConfigsService
}

// all
func (hd payConfigsHandler) all(c *gin.Context) {
	res, err := hd.srv.All()
	response.CheckAndRespWithData(c, res, err)
}

// list configs列表
func (hd payConfigsHandler) list(c *gin.Context) {
	var page request.PageReq
	var listReq req.PayConfigsListReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &page)) {
		return
	}
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &listReq)) {
		return
	}
	res, err := hd.srv.List(page, listReq)
	response.CheckAndRespWithData(c, res, err)
}

// detail configs详情
func (hd payConfigsHandler) detail(c *gin.Context) {
	var detailReq req.PayConfigsDetailReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyQuery(c, &detailReq)) {
		return
	}
	res, err := hd.srv.Detail(detailReq.Id)
	response.CheckAndRespWithData(c, res, err)
}

// add configs新增
func (hd payConfigsHandler) add(c *gin.Context) {
	var addReq req.PayConfigsAddReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &addReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Add(addReq))
}

// edit configs编辑
func (hd payConfigsHandler) edit(c *gin.Context) {
	var editReq req.PayConfigsEditReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &editReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Edit(editReq))
}

// del configs删除
func (hd payConfigsHandler) del(c *gin.Context) {
	var delReq req.PayConfigsDelReq
	if response.IsFailWithResp(c, util.VerifyUtil.VerifyBody(c, &delReq)) {
		return
	}
	response.CheckAndResp(c, hd.srv.Del(delReq.Id))
}
